<?php



namespace app\api\controller\pifa;





use app\common\controller\Api;

use app\common\library\Ems;

use app\common\library\Sms;

use fast\Random;

use think\Config;

use think\Db;

use think\Validate;

use think\Session;

use addons\third\library\Service;

use addons\epay\library\Service as PayService;

use addons\third\library\Wechat;

use Yansongda\Pay\Exceptions\GatewayException;

use Yansongda\Pay\Pay;

use addons\third\model\Third;

use app\common\library\Auth;

use app\api\library\Order as PayOrder;

use think\Request;

use fast\Http;

use think\Exception;

use think\Log;

use fast\WechatMini;

use app\api\model\UserAddress;

/**



 * 会员接口



 */



class Addres extends Api



{



    protected $noNeedLogin = ['login','getconfig','token','getBlock','getgoods','getfenlei'];

    protected $noNeedRight = '*';

    protected $config = null;

    protected $token = '';

    public function _initialize()

    {

        $this->token = $this->request->post('token');



        parent::_initialize();



    } 



   public function getmoren(){

      



        $res = UserAddress::getUserDefaultAddress();

      

        if (!empty($res)) {

           $this->success("请求成功", $res);

        }else{

            $this->error("请求成功");

        }

       

    }



    //地址筛选初始化

    public function addressinit()

    {

            

        $map['pid'] = 0;

        // $map['status'] = 'normal';

        $province = Db::table('fa_area')->where($map)->cache(true)->order('id asc')->select();

        $map['pid'] = $province[0]['id'];

        $city = Db::table('fa_area')->where($map)->cache(true)->order('id asc')->select();

        $map['pid'] = $city[0]['id'];

        $area = Db::table('fa_area')->where($map)->cache(true)->order('id asc')->select();



        $resdata[0] = $province;

        $resdata[1] = $city;

        $resdata[2] = $area;

        $this->success("请求成功", $resdata);

    }

    //获取城市地区

    public function getcity()

    {

        $post = $this->request->request();        

        $map['pid'] = $post['pid'];

        // $map['status'] = 'normal';

        $city = Db::table('fa_area')->where($map)->cache(true)->order('id asc')->select();

        $map['pid'] = $city[0]['id'];

        $area = Db::table('fa_area')->where($map)->cache(true)->order('id asc')->select();

        $resdata[0] = $city;

        $resdata[1] = $area;

        $this->success("请求成功", $resdata);

    }

    

    //我的地址详情

    public function adressshow(){

        $post = $this->request->request();  



        $params['id'] = $post['id'];



        $info = UserAddress::info($params);



        $info['diqu'] = $info['province_name'].'-'.$info['city_name'].'-'.$info['area_name'];



       
 

        $info = $info->toArray();


        $info['fileList1'][0]['url'] = cdnurl($info['image'],true);

      

        $this->success('地址详情', $info);





    }



     //获取城市地区

    public function getarea()

    {

        $post = $this->request->request();        

        $map['pid'] = $post['pid'];

        // $map['status'] = 'normal';

        $area = Db::table('fa_area')->where($map)->cache(true)->order('id asc')->select();

        $this->success("请求成功", $area);

    }



    //默认收货地址



    public function moren()

    {

        $post = $this->request->request(); 



        $params['id'] = $post['id'];

        $params['is_default'] = '1';

   

        $this->success("默认成功", UserAddress::moren($params));

       

    }







    //默认收货地址



    public function delete()

    {

        $post = $this->request->request(); 

      

       

        $map['id'] = $post['id'];

        $map['user_id'] = $this->auth->id;

        $map['is_default'] = '1';

        $ret = UserAddress::where($map)->find();





        if (!empty($ret)) {

            $this->error("默认地址无法删除");

        }





        $params['id'] = $post['id'];

        $this->success("删除成功",UserAddress::del($params));

         

       

       

    }



    //添加修改收货地址

    public function editadress(){

        $post = $this->request->request();

        





        $rule = [

                'md_name'  => 'require',
                'image'  => 'require',
                'consignee'  => 'require',

                'phone'  => 'require|regex:/^1\d{10}$/',

                // 'province_id'     => 'require',

                'lng'     => 'require',

        ];

        $msg = [         

                'md_name.require' => '请输入店铺名称',
                'image.require' => '请上传门头照片',
                'consignee.require' => '请输入姓名',

                'phone.require' => '请填写手机号',

                'phone.regex' => '请输入正确的手机号',
                'lng.require' => '请选择详细位置',

              

        ];

        $data = $post;

        $validate = new Validate($rule, $msg);

        $result = $validate->check($data);

        if (!$result) {

            $this->error(__($validate->getError()));

        }
        $post['image'] =  str_ireplace($this->request->root(true), '', $post['image']);

      

        if (empty($post['id'])) {

            $params['user_id'] = $this->auth->id;

            $params['consignee'] = $post['consignee'];

            $params['phone'] = $post['phone'];

            $params['area_text'] = $post['diqu'];

            $params['address'] = $post['address'];
            $params['image'] = $post['image'];

            $params['latitude'] = $post['lat'];

            $params['longitude'] = $post['lng'];
            $params['md_name'] = $post['md_name'];
            $params['xiangxi'] = $post['xiangxi'];

            $params['id'] = 0;



            $params['is_default'] = '1';

        

            $this->success("添加成功",UserAddress::edit($params));

        }else{

        

            $params['user_id'] = $this->auth->id;

            $params['consignee'] = $post['consignee'];

            $params['phone'] = $post['phone'];

            $params['area_text'] = $post['diqu'];

            $params['address'] = $post['address'];
            $params['md_name'] = $post['md_name'];
            $params['image'] = $post['image'];

            $params['latitude'] = $post['lat'];

            $params['longitude'] = $post['lng'];
            $params['xiangxi'] = $post['xiangxi'];

            $params['id'] = $post['id'];

            $params['is_default'] = '1';



            $this->success('编辑成功', UserAddress::edit($params));

        }





    }

    





    //获取地址列表

    public function addresslist()

    {

            



        $map['user_id'] = $this->auth->id;



        $list = UserAddress::getUserAddress();







        $this->success("请求成功", $list);

    }











    



    



   



}



